﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _430GroupProject;

namespace WebClient
{
    public partial class TeamScheduling : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }
        }

        protected void LoadNewData(object sender, EventArgs e)
        {

        }

        protected void TeamSelect_Load(object sender, EventArgs e)
        {
            TeamSelect.EnableViewState = true;
            User user = (User)Session["user"];
            if (user != null)
            {
                List<string> toManage = new List<string>();
                if (user.Permissions.Contains(Permission.Admin))
                {
                    toManage = Master.Proxy.getAllWorkgroupNames();
                }
                else
                {
                    toManage = user.WorksIn;
                }
                

                TeamSelect.DataSource = toManage;
                TeamSelect.DataBind();
                TeamSelect.SelectedValue = toManage[0];
                ViewState["selectedTeam"] = (string)TeamSelect.SelectedValue;
                LoadSchedules(TeamSelect.SelectedValue);
            }
            else
            {

            }
        }

        private void LoadSchedules(string teamName)
        {
            WorkGroup wg = Master.Proxy.getWorkGroup(teamName);
            List<string> scheduleNames = new List<string>();
            if (wg.Schedules == null)
            {
                wg.Schedules = new List<Schedule>();
            }
            foreach (Schedule s in wg.Schedules)
            {
                scheduleNames.Add(s.Name);
            }
            scheduleNames.Add("Create New");
            ScheduleSelect.DataSource = scheduleNames;
            ScheduleSelect.DataBind();
            ScheduleSelect.SelectedValue = scheduleNames[0];
            ViewState["selectedSchedule"] = (string)ScheduleSelect.SelectedValue;

            LoadDataGrid(ScheduleSelect.SelectedValue);
            
        }

        private void LoadDataGrid(string scheduleName)
        {
            if (scheduleName.Equals("Create New") || scheduleName.Equals(""))
            {
                ViewSchedulesPanel.Visible = false;
                CreateSchedulePanel.Visible = true;
            }
            else
            {
                ViewSchedulesPanel.Visible = true;
                CreateSchedulePanel.Visible = false;
                Schedule current = Master.Proxy.getSchedule(ViewState["selectedSchedule"] as string);
                if (current.TimeSlots == null)
                {
                    current.TimeSlots = new List<TimeSlot>();
                }
                ScheduleGrid.DataSource = current.TimeSlots;
                ScheduleGrid.DataBind();
            }
        }

        protected void CreateSchedule_Click(object sender, EventArgs e)
        {
            Schedule toAdd = new Schedule(ScheduleName.Text);
            WorkGroup toUpdate = Master.Proxy.getWorkGroup(ViewState["selectedTeam"] as String);
            if (toUpdate.Schedules == null)
            {
                toUpdate.Schedules = new List<Schedule>();
            }
            toUpdate.Schedules.Add(toAdd);
            WorkGroup verify = Master.Proxy.updateWorkGroup(toUpdate);
            LoadSchedules(ViewState["selectedTeam"] as String);
            //UpdatePanel.Update();
            Response.Redirect(Request.RawUrl);
        }

        protected void AddNewTimeSlot_Click(object sender, EventArgs e)
        {
            AddTimeSlotPanel.Visible = true;
        }

        protected void AddTimeSlot_Click(object sender, EventArgs e)
        {
            int endhrs = Convert.ToInt32(EndHrs.Text);
            int endmins = Convert.ToInt32(EndMins.Text);
            int strhrs = Convert.ToInt32(StartHrs.Text);
            int strmins =  Convert.ToInt32(StartMins.Text);

            TimeSlot toAdd = new TimeSlot(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, strhrs, strmins, 0), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, endhrs,endmins, 0), Convert.ToInt32(NumParticipants.Text));
            
            WorkGroup toUpdate = Master.Proxy.getWorkGroup(ViewState["selectedTeam"] as String);
            foreach (Schedule s in toUpdate.Schedules)
            {
                if (s.Name.Equals(ViewState["selectedSchedule"] as String))
                {
                    toUpdate.Schedules.Remove(s);
                    if (s.TimeSlots == null)
                    {
                        s.TimeSlots = new List<TimeSlot>();
                    }
                    s.TimeSlots.Add(toAdd);
                    toUpdate.Schedules.Add(s);
                    break;
                }
            }

            WorkGroup verify = Master.Proxy.updateWorkGroup(toUpdate);
            Response.Redirect(Request.RawUrl);
            //UpdatePanel.Update();


        }

        protected void ScheduleGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = ScheduleGrid.Rows[index];

            if (e.CommandName == "Register")
            {
                WorkGroup toUpdate = Master.Proxy.getWorkGroup(ViewState["selectedTeam"] as String);
                List<Schedule> curSchedules= toUpdate.Schedules;
                foreach (Schedule s in curSchedules)
                {
                    if (s.Name.Equals(ViewState["selectedSchedule"] as String))
                    {
                        foreach (TimeSlot ts in s.TimeSlots)
                        {
                            if (ts.TimeSlotID.ToString().Equals(row.Cells[1].Text))
                            {
                                User user = (User)Session["user"];
                                toUpdate.Schedules.Remove(s);
                                s.TimeSlots.Remove(ts);
                                Schedule updateS = s;
                                toUpdate.Schedules.Remove(s);
                                TimeSlot updateTs = Master.Proxy.getTimeSlot(ts.TimeSlotID);
                                updateTs.AssignedUsers.Add((User)Session["user"]);
                                updateTs.AssignedUsersList += "[" + user.NetID + "]";
                                Master.Proxy.upDateTimeSlot(updateTs);
                                updateS.TimeSlots.Add(updateTs);
                                toUpdate.Schedules.Add(updateS);
                                Master.Proxy.updateWorkGroup(toUpdate);

                                Response.Redirect(Request.RawUrl);
                                return;
                                
                            }
                        }
                    }
                }
            }


        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }

    }
}